package jface;

import jface.data.Person;
import jface.data.Person.Gender;
import jface.util.PersonLabelProvider;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;

public class JFaceTableExample {
	
	static Person[] persons = new Person[10];;
	
	
	public static void main(String[] args) {
		
		for (int i = 0; i < persons.length; i++) {
			persons[i] = new Person("First "+i, "Last "+i, i*7, i > 5 ? Gender.female : Gender.male);
		}
		
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

		String[] columnNames = new String[] { "First Name", "Last Name", "Age", "Gender" };
		
		TableViewer tableViewer = new TableViewer(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.getTable().setLinesVisible(true);

		for (String columnName : columnNames) {
			TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer,SWT.NONE);
			TableColumn tableColumn = tableViewerColumn.getColumn();
			tableColumn.setText(columnName);
			tableColumn.setResizable(true);
			tableColumn.setMoveable(true);
			tableColumn.pack();
		}

		tableViewer.setLabelProvider(new PersonLabelProvider());
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setInput(persons);
		
		tableViewer.getTable().pack();
		shell.pack();
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		display.dispose();
	}

}
